package servlet.vehicle;

import bean.dto.VehicleChangeData;
import bean.pojo.ParkingSpace;
import bean.pojo.User;
import bean.pojo.Vehicle;
import bean.pojo.Visitor;
import bean.vo.ParkingInfo;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.ParkingSpaceService;
import service.VehicleService;
import service.VisitorService;
import service.impl.ParkingSpaceServiceImpl;
import service.impl.VehicleServiceImpl;
import service.impl.VisitorServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * servlet.vehicle
 * User: 坚持
 * Date: 2024/12/19 14:14
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
@WebServlet(name="ParkingSpaceManagementServlet",value = "/parkingSpace/*")
public class ParkingSpaceManagementServlet extends BaseServlet {

    public R update(HttpServletRequest request){
        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data=bufferedReader.readLine();
            ParkingSpace parkingSpace = JSONObject.parseObject(req_data,ParkingSpace.class);

            ParkingSpaceService parkingSpaceService = new ParkingSpaceServiceImpl();

            Boolean update = parkingSpaceService.update(parkingSpace);
            //在Dao层或者Service层判断是否回滚事务
            return update?R.ok():R.error("修改失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    public R findBy(HttpServletRequest request){
        Integer page = NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit = NumberFormat.stringToInteger(request.getParameter("limit"));
        Integer ownerVehicleId = NumberFormat.stringToInteger(request.getParameter("ownerVehicleId"));
        Integer parkingVehicleId = NumberFormat.stringToInteger(request.getParameter("parkingVehicleId"));

        ParkingSpaceService parkingSpaceService = new ParkingSpaceServiceImpl();

        Long total = parkingSpaceService.findByTotal(ownerVehicleId,parkingVehicleId);
        List<ParkingInfo> list=null;
        if(total>0) {
            //当前页码的数据
            list = parkingSpaceService.findBy(page,limit,ownerVehicleId,parkingVehicleId);
        }
        return R.ok().addData("total",total).addData("list",list);
    }
}
